var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    invitationList : '',
    inRecordHeight :'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');      //用户的token信息

    that.setData({
      token: token
    })
    that.initInvitation(token,0,20); //初始化邀请记录
    that.getHeight();
  },

  /**
   * 初始化邀请记录
   */
  initInvitation: function (token, start, length){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/inviteRecord", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        start: start,
        length: length,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        console.log(res)
        if (code == "10000") {
          var invitationData = res.data.data.list;
          that.ordersPaging(start,invitationData)
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 下拉
   */
  scrollLower: function () {
    var that = this;
    var token = that.data.token;
    var start = that.data.startFlag;
    that.initInvitation(token, start, 20);
  },

  /**
   * 商品分页
   */
  ordersPaging: function (start, invitationData) {
    var that = this;
    if (start > 0) {
      var invitationList = that.data.invitationList;
      // 追加数据
      for (var i = 0; i < invitationData.length; i++) {
        invitationList.push(invitationData[i])
      }
      start = parseInt(start) + 1;
    } else {
      var invitationList = invitationData;
      start = 1;
    }
    that.setData({
      invitationList: invitationList,
      startFlag: start
    })
  },

  /**
   * 单项高度
   */
  getHeight: function () {
    var that = this;
    var query = wx.createSelectorQuery();

    var inRecordThConHeight = "";

    query.select('#inRecordThCon').boundingClientRect()
    query.exec(function (res) {
      inRecordThConHeight = res[0].height;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            inRecordHeight: res.windowHeight - inRecordThConHeight - 26
          });
        }
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})